<template>
    <view class="content" style="position: relative;">
        <!-- 顶部导航 -->
        <view >
            <HomeNavbar></HomeNavbar>
        </view>
		
		<view class="redbg" :style="{top:statusHeight+'px'}">
		</view>
		
		<!-- <view style="position: relative;top: -479upx;"> -->
		<view style="margin:20upx 0 80upx;">
			<!-- 首页板块 -->
			<template v-for="item in homeData">
			    <MmvHomeTemplate :key="item.components" :isComponents="item.components" :homeData="homeData"></MmvHomeTemplate>
			</template>
		</view>
        
        <!-- 底部导航 -->
        <HomeTabbar :tab="0"></HomeTabbar>
        <!-- 登录弹窗 -->
        <LoginMask></LoginMask>
		
		<HomeCouponDialog></HomeCouponDialog>
		
		<HomeSelectStore></HomeSelectStore>
		
		
		
		<a-tip isCustom></a-tip>
    </view>
</template>

<script>
import HomeNavbar from '@/components/layout/navbar.vue';
import MmvHomeTemplate from './components/mmv-home-template.vue';
import HomeTabbar from '@/components/layout/tabbar.vue';
import LoginMask from '@/components/layout/login-mask.vue';
import HomeCouponDialog from './components/home-coupon-dialog.vue';
import HomeSelectStore from './components/home-select-store.vue';

import {strToObj} from '@/util/index.js';

import ATip from '@/components/aTip.vue';

export default {
    components: {
        HomeNavbar,
        MmvHomeTemplate,
        HomeTabbar,
        LoginMask,
		ATip,
		HomeCouponDialog,
		HomeSelectStore
    },
    data() {
        return {
			statusHeight:0,
        };
    },
    computed: {
        isShowLogin() {
            return this.$store.state.app.isShowLogin;
        },
        homeData() {
            return this.$store.state.home.homeData;
        },
        openid() {
            return this.$store.state.user.openid;
        },
        deptId() {
            return this.$store.state.user.deptId;
        },
        userInfo() {
            return this.$store.state.user.userInfo;
        },
    },
    async onLoad(op) {
		this.statusHeight = uni.getSystemInfoSync().statusBarHeight + 44;
		
		wx.showShareMenu({
			withShareTicket:true,
		  menus: ['shareAppMessage',]
		})
		
        this.$store.dispatch('home/getHomeData', '首页');
        // this.$store.dispatch('user/wxLogin');//获取openid
		
		
		const scene = decodeURIComponent(op.scene);
		
		console.log('解码后的scene', scene);
		
		if(!uni.$u.test.isEmpty(op.scene)){
			if(op.scene.indexOf('%')>-1){
				//scene是个对象，应该是门店二维码
				let op_qrcode = strToObj(scene);
				console.log('首页的op_qrcode', op_qrcode);
				
				// 从二维码获取门店id
				if (op_qrcode.deptId) {
					this.$store.commit('user/saveSourceType', 2);
				    this.$store.commit('user/savedeptId', op_qrcode.deptId);
					uni.setStorageSync('deptId', op_qrcode.deptId);
				}
				
				// 从二维码获取userid
				if (op_qrcode.userId) {
					this.$store.commit('user/saveSourceType', 2);
					this.$store.commit('user/saveuserId', op_qrcode.userId);
				}
				
				// 从二维码获取channelId
				if (op_qrcode.channelId) {
					this.$store.commit('user/saveSourceType', 2);
					this.$store.commit('user/savechannelId', op_qrcode.channelId);
				}
				
			}else{
				//scene值只是个数字，应该是邀请分享来的
				console.log('邀请赚钱来的', scene);
				// 存入分享上级
				this.$store.commit('user/saveCustomerId', scene);
			}
		}
		
		
		
		
		//分享有两种 1，直接进入首页    2，打开对应页面再点左上角回到首页
		
		//情况1 直接进入首页 保存分享人id和门店id
		if(!uni.$u.test.isEmpty(op.customerId)){
			this.$store.commit('user/saveCustomerId', op.customerId);
		}
		if(!uni.$u.test.isEmpty(op.deptId)){
			this.$store.commit('user/saveSourceType', 4);
			this.$store.commit('user/savedeptId', op.deptId);
			uni.setStorageSync('deptId', op.deptId);
		}
		
		
		
		
		//情况2  从活动等页面回到首页
		if(!uni.$u.test.isEmpty(getApp().globalData.tempDeptId)){
			
			this.$store.commit('user/saveSourceType', 4);
			
			console.log('首页收到了临时deptId', getApp().globalData.tempDeptId);
			this.$store.commit('user/savedeptId', getApp().globalData.tempDeptId);
			uni.setStorageSync('deptId', getApp().globalData.tempDeptId);
			getApp().globalData.tempDeptId = '';
		}
		
		if(!uni.$u.test.isEmpty(getApp().globalData.tempCustomerId)){
			
			this.$store.commit('user/saveCustomerId', getApp().globalData.tempCustomerId);
			getApp().globalData.tempCustomerId = '';
		}
		
		this.$store.commit('user/saveDeptChoseFlag', getApp().globalData.deptchose);

		
		//如果有deptId,不需要再选门店
		if(!uni.$u.test.isEmpty(this.$store.state.user.deptId)){
			// 获取门店信息
			await this.$store.dispatch('app/getStoreInfo', this.$store.state.user.deptId);
		}
		
		uni.$on('candeptchose', res=>{
			
			console.log('收到可切换门店通知');
			
			//如果deptId是空，弹框选择门店
			if(uni.$u.test.isEmpty(this.$store.state.user.deptId)){
				// 获取门店信息 
				
				console.log('无默认门店，获取门店');
				
				this.$store.dispatch('app/getStoreInfo', this.$store.state.user.deptId)
			}
		})
		
		
    },
	methods:{
		
		onShareAppMessage() {
			return {
				// title: '哈哈哈',
				// imageUrl: this.baseImageUrl + this.activityInfo.shareImage,
				path: '/pages/home/home?customerId=' + this.$store.state.user.userInfo.customerId + '&deptId='+this.$store.state.user.deptId
			}
		},
	}
};
</script>

<style lang="scss" scoped>
	.redbg{
		z-index: -99;
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		// position: sticky;
		// top: calc(44px + var(--status-bar-height));
		// top: 128rpx;
		// width: 100%;
		height: 500upx;
		background: linear-gradient(to bottom, #FA697C, #F895A3, #F5E0E3, #F8F8F8);
	}
	
</style>
